﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using Newtonsoft.Json.Linq;

namespace OperateCamera
{
    public partial class Form2 : Form
    {

        public Form1 parent;

        public Form2(Form1 f)
        {

            InitializeComponent();

            parent = f;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SQLiteDBHelper db = new SQLiteDBHelper(Environment.CurrentDirectory + "\\data\\database.db3");

            string y = this.textBox1.Text;

            if (y == "")
            {

                MessageBox.Show("请填写许可码！");
                return;
            }

            string x = common.GetDiskID();

            if (x == "unknow")
            {

                MessageBox.Show("激活过中出现问题，请检测你的网络是否连接良好");
                return;
            }


            string url = db.C("license_api")[0][1] + "?act=get_license&x=" + x + "&y=" + y;

            string _res = common.http(url);

            //如果没返回任何东西 提示网络问题
            if (_res == "")
            {

                MessageBox.Show("请确保网络是否正常，如果确定网络正常但收到这提示，请联系我们");

                return;
            }


            JObject _response;
            try
            {

                _response = JObject.Parse(_res);
            }
            catch (Exception msg)
            {

                MessageBox.Show("请忽修改授权地址，否则会导致系统不正确。错误提示：" + msg.Message);

                return;

            }

            if (_response["error"].ToString() == "1")
            {

                MessageBox.Show(_response["message"].ToString());
                return;
            }
            else
            {
                //成功
                _res = _response["content"].ToString();

                db.C("license", _res);
                db.C("license_code", y);

                MessageBox.Show("你已成功激活软件，谢谢您的使用！");

                parent.Opacity = 1;
                parent.ShowInTaskbar = true;

                this.Close();

            }
        }
    }
}
